<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <div class="loginlogo" />
      </div>
      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="account"
          v-model="loginForm.account"
          placeholder="账号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="code"
          v-model="loginForm.code"
          placeholder="验证码"
          name="code"
          type="text"
          tabindex="1"
          auto-complete="off"
        />
        <img class="code" @click="changeCode">
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >登录</el-button>
    </el-form>
    <div class="tips">
      <video src="../../assets//login_bg.mp4" autoplay muted loop />
    </div>
  </div>
</template>

<script>
import { getCode } from './api/login'
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        account: 'shenlingadmin',
        bindAccount: '',
        bindPassword: '',
        code: '45',
        key: '514FEF9237A05774D34A0F5B1694169133111',
        password: '123456',
        signAccount: '',
        signPassword: '',
        tenant: '0000'
      },
      loginRules: {
        account: [
          { required: true, trigger: 'change', message: '请输入账号' }
        ],
        password: [
          { required: true, trigger: 'change', message: '请输入密码' }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
  },
  methods: {
    ...mapActions('user', ['login']),
    changeCode() {
      this.getCode()
    },
    // 获取验证码
    async getCode() {
      this.loginForm.code = ''
      this.loginForm.key = Math.random().toString(16).slice(2) + Math.random().toString(16).slice(2)
      const res = await getCode({ key: this.loginForm.key })
      this.codeImg = res
      // 获取到的 Blob 数据
      const blobData = res
      // 创建 FileReader 对象
      const reader = new FileReader()
      // 读取 Blob 数据
      reader.readAsDataURL(blobData)
      // 当读取完成时
      reader.onloadend = function() {
        // 获取 Data URL
        const dataUrl = reader.result
        // 创建 Image 对象
        const image = new Image()

        // 图片加载完成时
        image.onload = function() {
          // 创建 Canvas 元素
          const canvas = document.createElement('canvas')
          const ctx = canvas.getContext('2d')

          // 设置 Canvas 的宽高为图片的宽高
          canvas.width = image.width
          canvas.height = image.height
          // 在 Canvas 上绘制图片
          ctx.drawImage(image, 0, 0)

          // 处理图像数据，例如上传到服务器或显示在页面上
          console.log(canvas.toDataURL())
          // 示例：将图像数据显示在页面上
          document.querySelector('.code').src = canvas.toDataURL()
        }

        // 设置 Image 的源为 Data URL
        image.src = dataUrl
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #999;
$cursor: #999;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  position: absolute;

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(176, 174, 174, 0.4);
    border-radius: 5px;
    color: #454545;

    &:focus {
      border: 1px solid #f56c6c;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #ccc;
$light_gray: #ccc;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    height: 490px;
    transform: translate(0,20%);
    max-width: 100%;
    background-color: #ffffff;
    padding: 20px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    z-index: 999;
  }

  .tips {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    background-color: #ccc;
    margin-bottom: 10px;

    video {
      width: 100%;
    }
  }
.code{
width: 71px;
height: 32px;
position: absolute;
right: 10px;
top: 23%;
cursor: pointer;
border: none;

}
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    width: 370px;
    height: 57px;
    margin-bottom: 28px;

    .loginlogo {
      margin: 0 auto;
      height: 54px;
      width: 195px;
      background-image: url(../../assets/loginLogo.png);
      background-size: cover;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  // 验证码样式
  .key {
    height: 100%;

    i {
      line-height: 47px;
    }
  }
}
</style>
